<?php

require_once(dirname(__FILE__) . '/appconf.php');

class DbMove extends Site {

    function setup() {
        $this->run_modes('do');
        $this->start_mode('do');
        $this->selfurl = '/dbMoveBlog.php';
        $this->pagesize = 200;

        set_time_limit(0);
        ob_end_clean();
        ob_implicit_flush(true);
        //$this->blog_array = array(
        //     array('uid' => 1086, 'blogid' => 79),
        //      array('uid' => 38, 'blogid' => 3),
        //      
        //      
        //      
        //      array('uid' => 40, 'blogid' => 2),
        //      array('uid' => 10454, 'blogid' => 267),
        //      array('uid' => 7096, 'blogid' => 264),
        //      array('uid' => 8787, 'blogid' => 272),
        //      
        //      
        //      
        //      array('uid' => 8899, 'blogid' => 197),
        /*
          array('uid' => 9194, 'blogid' => 202),
          array('uid' => 9097, 'blogid' => 199),
          array('uid' => 8120, 'blogid' => 52),
          array('uid' => 47, 'blogid' => 5),
          array('uid' => 8020, 'blogid' => 57),
          array('uid' => 403, 'blogid' => 55),
         * array('uid' => 4913, 'blogid' => 45),
          array('uid' => 5132, 'blogid' => 44),
          array('uid' => 5813, 'blogid' => 26),
          array('uid' => 2718, 'blogid' => 40),
          array('uid' => 6773, 'blogid' => 53),
          array('uid' => 601, 'blogid' => 41),
          array('uid' => 1299, 'blogid' => 25),
          array('uid' => 6025, 'blogid' => 47),
          array('uid' => 513, 'blogid' => 37),
          array('uid' => 405, 'blogid' => 254),
          array('uid' => 30, 'blogid' => 19),
          array('uid' => 458, 'blogid' => 7),
          array('uid' => 7302, 'blogid' => 110),
          array('uid' => 7617, 'blogid' => 262),
          array('uid' => 11335, 'blogid' => 263),
          array('uid' => 9458, 'blogid' => 266),

         * array('uid' => 2207, 'blogid' => 101),
          array('uid' => 7347, 'blogid' => 74),
          array('uid' => 6022, 'blogid' => 76),
          array('uid' => 7536, 'blogid' => 103),
          array('uid' => 6941, 'blogid' => 77),
          array('uid' => 5810, 'blogid' => 105),
          array('uid' => 2831, 'blogid' => 54),
          array('uid' => 738, 'blogid' => 107),
          array('uid' => 65, 'blogid' => 80),
          array('uid' => 7087, 'blogid' => 69),
          array('uid' => 4879, 'blogid' => 81),
          array('uid' => 306, 'blogid' => 111),
          array('uid' => 7173, 'blogid' => 108),
          array('uid' => 3863, 'blogid' => 82),
          array('uid' => 58, 'blogid' => 84),
          array('uid' => 2724, 'blogid' => 85),
          array('uid' => 3838, 'blogid' => 113),
          array('uid' => 7705, 'blogid' => 87),
          array('uid' => 6276, 'blogid' => 114),
          array('uid' => 2128, 'blogid' => 116),
          array('uid' => 1781, 'blogid' => 38),
          array('uid' => 1090, 'blogid' => 117),
          array('uid' => 9040, 'blogid' => 241),
          array('uid' => 9065, 'blogid' => 162),
          array('uid' => 9066, 'blogid' => 163),
          array('uid' => 9068, 'blogid' => 165),
          array('uid' => 9082, 'blogid' => 180),
          array('uid' => 7268, 'blogid' => 258),
          array('uid' => 12926, 'blogid' => 167),
          array('uid' => 9069, 'blogid' => 166),
          array('uid' => 8970, 'blogid' => 168),
          array('uid' => 8971, 'blogid' => 169),
          array('uid' => 12927, 'blogid' => 191),
          array('uid' => 12928, 'blogid' => 201),
          array('uid' => 12929, 'blogid' => 195),
          array('uid' => 9070, 'blogid' => 170),
          array('uid' => 12930, 'blogid' => 189),
          array('uid' => 12931, 'blogid' => 190),
          array('uid' => 9206, 'blogid' => 204),
         * array('uid' => 197, 'blogid' => 268),
          array('uid' => 11916, 'blogid' => 270),
          array('uid' => 7148, 'blogid' => 269),
          array('uid' => 8141, 'blogid' => 265),
         * array('uid' => 101, 'blogid' => 271),
         * array('uid' => 5683, 'blogid' => 273),
          array('uid' => 5680, 'blogid' => 274),
          array('uid' => 9104, 'blogid' => 259),
          array('uid' => 12159, 'blogid' => 275),
          array('uid' => 12074, 'blogid' => 276),
          array('uid' => 12581, 'blogid' => 277),
          array('uid' => 7914, 'blogid' => 278),
          array('uid' => 7303, 'blogid' => 279),
          array('uid' => 10700, 'blogid' => 282),
          array('uid' => 12348, 'blogid' => 283),
          array('uid' => 11337, 'blogid' => 284),
          array('uid' => 11675, 'blogid' => 285),


         */

        //      
        //);
        /*
          $this->blog_array = array(
          '1718' => 15,
          '20037' => 14,
          '20038' => 13,
          '20039' => 12,
          '20040' => 11,
          );
         * 
         */

        $this->blog_array = array(
            array('uid' => 40, 'blogid' => 2),
            array('uid' => 10454, 'blogid' => 267),
            array('uid' => 7096, 'blogid' => 264),
            array('uid' => 8787, 'blogid' => 272),
        );

        parent::setup();
    }

    function cgiapp_prerun() {

        require_once(LIB_PATH . '/Common/HTMLPurifier/HTMLPurifier.standalone.php');
        parent::cgiapp_prerun();
    }

    function action_do() {

        $page = $this->get_q('page', '1', 'int');
        $root = $this->param('WWW');

        $num = count($this->blog_array);
        if ($page > $num) {
            echo '完成全部轉換';
            exit();
        }
        $value = $this->blog_array[($page - 1)];


        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        echo $value['blogid'];
        echo "<br>";


        $param = array();
        $param['field'] = "TB1.name,TB1.term_id,TB2.parent";
        $param['where'] = "TB2.parent=0";
        $param['order'] = "";
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "left join wp_" . $value['blogid'] . "_term_taxonomy";
        $param['on'] = "TB1.term_id = TB2.term_id";
        $data_terms_0 = db_query_hash('wp_' . $value['blogid'] . '_terms', $param, IMOREBLOGDB);
        if ($data_terms_0['result']) {
            foreach ($data_terms_0['result'] as $value_terms) {
                $attr = array();
                $attr['key_cat'] = $value['blogid'] . '_' . $value_terms['term_id'];
                $attr['mid'] = $value['uid'];
                $attr['name'] = $value_terms['name'];
                $attr['topid'] = 0;
                db_add('blog_category', $attr);
            }
        }

        $param = array();
        $param['field'] = "TB1.name,TB1.term_id,TB2.parent";
        $param['where'] = "TB2.parent>0";
        $param['order'] = "";
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "left join wp_" . $value['blogid'] . "_term_taxonomy";
        $param['on'] = "TB1.term_id = TB2.term_id";
        $data_terms_1 = db_query_hash('wp_' . $value['blogid'] . '_terms', $param, IMOREBLOGDB);

        if ($data_terms_1['result']) {
            foreach ($data_terms_1['result'] as $value_terms_1) {

                $param = array();
                $param['field'] = "id";
                $param['where'] = "key_cat='" . $value['blogid'] . "_" . $value_terms_1['parent'] . "'";
                $param['order'] = "";
                $param['group'] = "";
                $param['limit'] = "";
                $param['pagesize'] = "";
                $param['page'] = "";
                $param['join'] = "";
                $param['on'] = "";
                $data_category = db_query_hash('blog_category', $param);

                if ($data_category['result']) {
                    $attr = array();
                    $attr['key_cat'] = $value['blogid'] . '_' . $value_terms_1['term_id'];
                    $attr['mid'] = $value['uid'];
                    $attr['name'] = $value_terms_1['name'];
                    $attr['topid'] = $data_category['result'][0]['id'];
                    db_add('blog_category', $attr);
                }
            }
        }

        $param = array();
        $param['field'] = "";
        $param['where'] = " post_status='publish' and post_type='post'";
        $param['order'] = " post_date asc";
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('wp_' . $value['blogid'] . '_posts', $param, IMOREBLOGDB);



        if ($data['result']) {


            $str = "";
            $img_array = array();

            $config = HTMLPurifier_Config::createDefault();
            $config->set('HTML.TargetBlank', true);
            $config->set('HTML.Allowed', 'span[style],em,strong,img[src|align|height|width|alt|style],br,b');
            $HTMLPurifierObj = new HTMLPurifier($config);
//保留指定標籤，和標籤的屬性，其它全部過濾

            foreach ($data['result'] as $key_post => $value_post) {
                $value_post['ID'] . " , ";
                $content = $HTMLPurifierObj->purify($value_post['post_content']);

                $reg = "/<img[^>]*src=\"(http:\/\/(.+)\/(.+)\.(jpg|gif|bmp|png))\"/isU";
                preg_match_all($reg, $content, $img_array, PREG_PATTERN_ORDER);
                $img_array = array_unique($img_array[1]);

                if ($img_array) {
                    foreach ($img_array as $value_imgName) {
                        $ret = uploadfiles($root . '/upload/blog', $value_imgName);

                        if ($ret[0]['code'] == 1) {
                            $imgstr = str_replace($root, '', $ret[0]['path']);
                            $content = str_replace($value_imgName, $imgstr, $content);
                        }
                    }
                }


                $out = array();
                $pattern = '/\[gallery(.*?)\]/i';
                preg_match_all($pattern, $content, $out, PREG_PATTERN_ORDER);
                if (!empty($out[0][0])) {
                    $param = array();
                    $param['field'] = "";
                    $param['where'] = " post_status='inherit' and post_type='attachment' and post_parent=" . $value_post['ID'];
                    $param['order'] = " ID asc";
                    $param['group'] = "";
                    $param['limit'] = "";
                    $param['pagesize'] = "";
                    $param['page'] = "";
                    $param['join'] = "";
                    $param['on'] = "";
                    $data_img = db_query_hash('wp_' . $value['blogid'] . '_posts', $param, IMOREBLOGDB);
                    if ($data_img['result']) {
                        $str = "";

                        foreach ($data_img['result'] as $value_img) {

                            $ret = uploadfiles($root . '/upload/blog', $value_img['guid']);

                            if ($ret[0]['code'] == 1) {

                                $imgstr = str_replace($root, '', $ret[0]['path']);
                                $str .= " <img src=\"" . $imgstr . "\"> ";
                            }
                        }
                    }
                }

                $pattern_caption = '/\[caption(.*?)caption="(.*?)"(.*?)\](.*?)src="(.*?)"(.*?)caption\]/i';
                $replacement_caption = '<img src="${5}" /><br>${2}';
                $content = preg_replace($pattern_caption, $replacement_caption, $content);

                $pattern_s = '/\[gallery(.*?)\]/i';
                $replacement = '';
                $content = preg_replace($pattern_s, $replacement, $content);

                $insert_content = nl2br($content) . $str;

                $attr = array();
                $attr['mid'] = $value['uid'];
                $attr['title'] = $value_post['post_title'];
                $attr['content_json'] = "";
                $attr['body'] = $insert_content;
                $attr['is_valid'] = 1;
                $attr['is_accept'] = 1;
                $attr['status'] = 1;
                $attr['addtime'] = $value_post['post_date'];
                $attr['is_publish'] = 1;
                $attr['is_blog'] = 1;
                $id = db_add('product_reports', $attr);


                $param = array();
                $param['field'] = "term_taxonomy_id";
                $param['where'] = "object_id=" . $value_post['ID'];
                $param['order'] = "";
                $param['group'] = "";
                $param['limit'] = "";
                $param['pagesize'] = "";
                $param['page'] = "";
                $param['join'] = "";
                $param['on'] = "";
                $data_term_relationships = db_query_hash('wp_' . $value['blogid'] . '_term_relationships', $param, IMOREBLOGDB);
                if ($data_term_relationships['result']) {
                    foreach ($data_term_relationships['result'] as $value_term_relationships) {
                        $attr = array();
                        $attr['rid'] = $id;

                        $param = array();
                        $param['field'] = "id";
                        $param['where'] = "key_cat='" . $value['blogid'] . "_" . $value_term_relationships['term_taxonomy_id'] . "'";
                        $param['order'] = "";
                        $param['group'] = "";
                        $param['limit'] = "";
                        $param['pagesize'] = "";
                        $param['page'] = "";
                        $param['join'] = "";
                        $param['on'] = "";
                        $data_terms = db_query_hash('blog_category', $param);

                        $attr['cid'] = $data_terms['result'][0]['id'];
                        db_add('blog_otm_category', $attr);
                    }
                }


                echo $key_post . " 成功 " . $value_post['ID'] . " : " . $value_post['post_title'] . "<br>";
                flush();    //刷新输出缓冲 
            }

            echo "換頁";
            echo '<meta http-equiv="refresh" content="10;url= http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?action=do&page=' . ($page + 1) . '">';
        } else {
            echo "無數據";
            echo '<meta http-equiv="refresh" content="10;url= http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?action=do&page=' . ($page + 1) . '">';
        }
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new DbMove();
$webapp->run();
?>